<template>
  <div class="employee-detail">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="登录账户设置" name="loginSet">
            <el-form
              ref="loginSetForm"
              :model="loginSetForm"
              label-width="80px"
              style="margin-top:30px;margin-left:120px"
              :rules="rules"
            >
              <el-form-item label="姓名" prop="username">
                <el-input
                  v-model="loginSetForm.username"
                  style="width:300px"
                  placeholder="请输入姓名"
                ></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="newPassword">
                <el-input
                  v-model="loginSetForm.newPassword"
                  type="password"
                  style="width:300px"
                  placeholder="请输入密码"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserDetail">更新</el-button>
                <el-button type="default" @click="$router.back()">
                  取消
                </el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="userDetail">
            <!-- 打印 -->
            <router-link :to="`/employees/print/${id}?type=personal`">
              <el-tooltip effect="dark" content="打印个人信息" placement="top">
                <i class="el-icon-printer"></i>
              </el-tooltip>
            </router-link>
            <!-- 个人信息组件 -->
            <user-info ref="userInfo" @updateUser="loadDetail" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="postMsg">
            <!-- 打印 -->
            <router-link :to="`/employees/print/${id}?type=job`">
              <el-tooltip effect="dark" content="打印个人信息" placement="top">
                <i class="el-icon-printer"></i>
              </el-tooltip>
            </router-link>
            <!-- 岗位信息组件 -->
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetUserDetailById } from '@/api/user'
import { reqUpdateUserDetail } from '@/api/employees'
import UserInfo from './component/user-info'
import JobInfo from './component/job-info'

export default {
  name: 'EmployeeDetail',
  components: { UserInfo, JobInfo },
  props: {
    id: {
      type: String,
      default: '001',
    },
  },
  data() {
    return {
      activeName: 'loginSet', // tab栏切换
      // 登录账户表单数据
      loginSetForm: {
        username: '',
        password: '',
        newPassword: '',
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' },
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] },
        ],
      },
    }
  },
  created() {
    // 加载用户详情
    this.loadDetail()
  },
  methods: {
    // 一、获取用户详情
    async loadDetail() {
      const data = await reqGetUserDetailById(this.id)
      // console.log(data)
      this.loginSetForm = data
      this.loginSetForm.password = ''
    },

    // 二、更新用户信息
    updateUserDetail() {
      this.$refs['loginSetForm'].validate(async valid => {
        if (valid) {
          await reqUpdateUserDetail({
            ...this.loginSetForm,
            password: this.loginSetForm.newPassword,
          })
          this.$message.success('更新成功')
          // 更新store中的数据,更新user-info中的用户信息
          this.$store.dispatch('user/getUserInfoAsync')
          this.$refs.userInfo.getUserDetailById()
        } else {
          this.$message.error('请填写必须项')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.el-form {
  margin: 30px 0 0 120px;
}
.el-icon-printer {
  position: absolute;
  right: 50px;
}
</style>
